import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * CyclicBarrier 控制固定的n个任务为一组
 * 当某一线程做完了相应的任务就(await)后面的不执行,等待n个全部完成了才算任务完成,
 * 后面的才执行
 * 可以循环使用(cyclic)
 */
public class CyclicBarrierTest {

	public static void main(String[] args) {
		final CyclicBarrier cyclicBarrier = new CyclicBarrier(4,new Runnable(){
			public void run() {
				System.out.println("四个地点的旅行结束!");
			}
		});
		
		ExecutorService executorService = Executors.newCachedThreadPool();
		
		for(int i=0;i<8;i++){
			executorService.execute(new Runnable(){
				public void run() {
					try {
						Thread.sleep((long)Math.random()*10000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					System.out.println("线程"+Thread.currentThread().getName()+
							"即将到达1,已有"+cyclicBarrier.getNumberWaiting()+
							"个到达,"+(cyclicBarrier.getNumberWaiting() == cyclicBarrier.getParties()-1 ? "到齐了,继续走.":"等候..."));
					
					try {
						cyclicBarrier.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (BrokenBarrierException e) {
						e.printStackTrace();
					}
					
					/*System.out.println("线程"+Thread.currentThread().getName()+
							"即将到达2,已有"+cyclicBarrier.getNumberWaiting()+
							"个到达,"+(cyclicBarrier.getNumberWaiting() == cyclicBarrier.getParties()-1 ? "到齐了,继续走.":"等候..."));
					
					try {
						cyclicBarrier.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (BrokenBarrierException e) {
						e.printStackTrace();
					}
					
					System.out.println("线程"+Thread.currentThread().getName()+
							"即将到达3,已有"+cyclicBarrier.getNumberWaiting()+
							"个到达,"+(cyclicBarrier.getNumberWaiting() == cyclicBarrier.getParties()-1 ? "到齐了,继续走.":"等候..."));
					
					try {
						cyclicBarrier.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (BrokenBarrierException e) {
						e.printStackTrace();
					}
					
					System.out.println("线程"+Thread.currentThread().getName()+
							"即将到达4,已有"+cyclicBarrier.getNumberWaiting()+
							"个到达,"+(cyclicBarrier.getNumberWaiting() == cyclicBarrier.getParties()-1 ? "到齐了,继续走.":"等候..."));
					
					try {
						cyclicBarrier.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (BrokenBarrierException e) {
						e.printStackTrace();
					}
					
					System.out.println("线程"+Thread.currentThread().getName()+
							"即将到达5,已有"+cyclicBarrier.getNumberWaiting()+
							"个到达,"+(cyclicBarrier.getNumberWaiting() == cyclicBarrier.getParties()-1 ? "到齐了,继续走.":"等候..."));
					
					try {
						cyclicBarrier.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (BrokenBarrierException e) {
						e.printStackTrace();
					}*/
					
				}
			});
		}
		
		executorService.shutdown();
	}

}
